package com.kuang.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kuang.pojo.User;
import com.kuang.utils.JsonUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//@Controller
@RestController
public class UserController {
    //produces:指定响应体返回类型和编码
    @RequestMapping(value = "/j1")
    //@ResponseBody //不去找视图解析器，直接返回一个字符串
    public String json1() throws JsonProcessingException {

        //jakson
        ObjectMapper mapper = new ObjectMapper();


        //创建一个对象
        User user = new User("清姜1号",3,"男性");
        User user1 = new User("清姜2号",3,"男性");
        User user2 = new User("清姜3号",3,"男性");
        User user3= new User("清姜4号",3,"男性");

        String str = mapper.writeValueAsString(user);


        return str;
    }


    @RequestMapping(value = "/j2")
    //@ResponseBody //不去找视图解析器，直接返回一个字符串
    public String json2() throws JsonProcessingException {

        //jakson
        ObjectMapper mapper = new ObjectMapper();

        ArrayList<User> usersList = new ArrayList<User>();
        //创建一个对象
        User user1 = new User("清姜1号",3,"男性");
        User user2 = new User("清姜2号",3,"男性");
        User user3 = new User("清姜3号",3,"男性");
        User user4= new User("清姜4号",3,"男性");

        usersList.add(user1);
        usersList.add(user2);
        usersList.add(user3);
        usersList.add(user4);

        String str = mapper.writeValueAsString(usersList);


        return JsonUtils.getJson(usersList);
    }







    @RequestMapping(value = "/j3")
    //@ResponseBody //不去找视图解析器，直接返回一个字符串
    public String json3() throws JsonProcessingException {

        Date date = new Date();

        return JsonUtils.getJson(date);
    }






    @RequestMapping(value = "/j4")
    //@ResponseBody //不去找视图解析器，直接返回一个字符串
    public String json4() throws JsonProcessingException {


        //创建一个对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        List<User> userList = new ArrayList<User>();
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);

        String string = JSON.toJSONString(userList);
        return string ;
    }
}
